from django.db import models


# Create your models here.

# class Book(models.Model):
#     id = models.AutoField(primary_key=True)  # id 会自动创建,可以手动写入
#     title = models.CharField(max_length=32)
#     price = models.DecimalField(max_digits=5, decimal_places=2)
#     publish = models.CharField(max_length=32)
#     pub_date = models.DateField()

class Book(models.Model):
    title = models.CharField(max_length=32)  # 书籍名称
    price = models.DecimalField(max_digits=5, decimal_places=2)  # 书籍价格
    publish = models.ForeignKey("Publish", on_delete=models.CASCADE)  # 出版社
    pub_date = models.DateField()  # 出版时间
    authors = models.ManyToManyField("Author")  # 作者


class Publish(models.Model):
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=64)
    # EmailField 数据类型是邮箱格式，底层继承 CharField，进行了封装，相当于 MySQL 中的 varchar
    email = models.EmailField()


class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.SmallIntegerField()
    au_detail = models.OneToOneField("AuthorDetail", on_delete=models.CASCADE)  # 作者详情


class AuthorDetail(models.Model):
    gender_choices = (
        (0, "女"),
        (1, "男"),
        (2, "保密"),
    )
    gender = models.SmallIntegerField(choices=gender_choices)
    tel = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)
    birthday = models.DateField()
